import Vue from 'vue'// 1.
import VueRouter from 'vue-router'// 2.导入路由
// import HelloWorld from '@/components/HelloWorld'

// import Home from '../components/Home'
// import About from '../components/About'
// import User from '../components/User'
// 设置路由懒加载es6
const Home = () => import('../components/Home')
const HomeNews = () => import('../components/HomeNews')
const HomeMessage = () => import('../components/HomeMessage')
const About = () => import('../components/About')
const User = () => import('../components/User')
const Profile = () => import('../components/Profile')

Vue.use(VueRouter)// 3.通过Vue.use()安装插件

// 4.创建VueRouter对象
const routes = [// 5.配置路由映射
  {
    path: '/', // 6.重定向设置路由默认值
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '', // 配置默认值
        redirect: 'news'
      },
      {
        path: 'news', // 这里不用加斜杠
        component: HomeNews
      },
      {
        path: 'message', // 这里不用加斜杠
        component: HomeMessage
      }
    ]
  },
  {
    path: '/about',
    component: About,
    meta: {
      title: '关于'
    },
    beforeEnter: (to, from, next) => {
      console.log('about beforeEnter')
      next()
    }
  },
  {
    path: '/user/:abc',
    component: User,
    meta: {
      title: '用户'
    }
  },
  {
    path: '/profile',
    component: Profile,
    meta: {
      title: '档案'
    }
  }
]
const router = new VueRouter({
  routes,
  mode: 'history' // 7.修改路由模式为history
})

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  // 从from跳转到to   to.matched[0].meta.title解决了路由的嵌套
  document.title = to.matched[0].meta.title
  console.log('++++')
  next()
})

// 后置钩子(hook)
router.afterEach((to, from) => {
  console.log('----')
})

// 3.将router对象传入到Vue实例
export default router
